package cn.bellychang.ood.mailsendproduct.good;

import cn.bellychang.ood.mailsendproduct.DBUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ChangLiang
 * @date 2020/5/12
 */
public class UserService {

    /**
     * 根据促销产品列表 获取用户列表
     * @param products
     * @return
     */
    public List<User> getUsers(List<Product> products){
        List<User> list = new ArrayList<>();
        //调用DAO相关的类从数据库中读取订阅产品的用户列表

        List<String> ids = new ArrayList<>();
        products.stream().forEach(product->ids.add(product.getId()));
        StringBuilder builder = new StringBuilder();
        ids.forEach(id->builder.append(id).append(","));
        String idsStr = builder.length() > 0 ? builder.substring(0, builder.length() - 1) : builder.toString();
        String sendMailQuery = "Select name from subscriptions "
                + "where product_id in (" + idsStr  + ") "
                + "and send_mail=1 ";
        System.out.println("loadQuery set");
        DBUtil.query(sendMailQuery);

        User user1 = new User();
        user1.setName("changliang");
        user1.setEmailAddress("changliang@hzfi.cn");
        user1.setSubscribedProducts(products);

        User user2 = new User();
        user2.setName("changliang2");
        user2.setEmailAddress("changliang2@hzfi.cn");
        user2.setSubscribedProducts(products.subList(0,2));


        list.add(user1);
        list.add(user2);
        return list;
    }
}
